package 设计模式;

import lombok.Data;

@Data
public class Singleton {
    private static volatile Singleton instance;
    private String name;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    instance.setName("张三疯");
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            Singleton instance = Singleton.getInstance();
            System.out.println("t1" + instance);
        }, "t1").start();
        Thread.sleep(1000);
        new Thread(() -> {
            Singleton instance = Singleton.getInstance();
            System.out.println("t2" + instance);
        }, "t2").start();
    }
}

